<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'home';
$return['ischecklogin']=cfc('core')->checklogin('','',true);
$this->model('member')->upmember();
$return['pluginset']['ailogo']=tomedia($return['pluginset']['ailogo']);
$return['basic']=$return['pluginset'];
if($_RQ['op']=='home'){
	if($_RQ['id']){
		$return['item']=pdo_get('jy_agentmcp_usergoods',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('请选择设备进入！','error','/index/');
	}
	$return['member']= $this->model('member')->getmember();
	$return['openid']= $_SESSION['openid'];
	$return['needup']=intval($_RQ['needup']);
	$return['params']=array('type'=>intval($_RQ['type']),'ismymp3'=>intval($_RQ['ismymp3']),'groupid'=>0,'voicesex'=>0,'yuyuantype'=>0);
	$return['post']=array('goodsid'=>intval($_RQ['id']),'images'=>array(),'id'=>$_RQ['id'],'iswww'=>1);
	$return['sharedata']=array(
		'title'=>$return['basic']['share_title'],
		'desc'=>$return['basic']['share_desc'],
		'link'=>custom_url('jy_agentmcp/index//fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>ATTACHMENT_URL.$return['basic']['share_image'],
	);
	set_html('title',$return['item']['title']);
	exi($return);
}
if($_RQ['op']=='getmp3log'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=$_RQ['keyword'];
	}
	if($_RQ['llm_model']){
		$params['llm_model like']='%'.trim($_RQ['llm_model']).'%';
		$return['params']['llm_model']=$_RQ['llm_model'];
	}
	$fields='';
	$orderby=array();
	$_RQ['page'] = $_RQ['page'] ? $_RQ['page'] : 1;
	$pindex = max(1, intval($_RQ['page']));
	if(intval($_RQ['num'])){
		$psize = intval($_RQ['num']);
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_agentmcp_mp3log',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		foreach($list as &$item){
			$item['logo']=tomedia($item['logo']);
			$item['desc']=cfc('tools')->substr($item['desc'],0,'38');
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	$return['list']=$list?$list:array();
	exi($return);
}
if($_RQ['op']=='getmp3list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=$_RQ['keyword'];
	}
	$fields='';
	$orderby=array();
	$_RQ['page'] = $_RQ['mp3page'] ? $_RQ['mp3page'] : 1;
	$pindex = max(1, intval($_RQ['page']));
	if(intval($_RQ['num'])){
		$psize = intval($_RQ['num']);
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_agentmcp_mp3',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['logo']=tomedia($item['logo']);
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	$return['list']=$list?$list:array();
	exi($return);
}
if($_RQ['op']=='getmymp3list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=$_RQ['keyword'];
	}
	if($_RQ['llm_model']){
		$params['llm_model like']='%'.trim($_RQ['llm_model']).'%';
		$return['params']['llm_model']=$_RQ['llm_model'];
	}
	$fields='';
	$orderby=array();
	$_RQ['page'] = $_RQ['page'] ? $_RQ['page'] : 1;
	$pindex = max(1, intval($_RQ['page']));
	if(intval($_RQ['num'])){
		$psize = intval($_RQ['num']);
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_agentmcp_usermp3',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['logo']=tomedia($item['logo']);
			$item['desc']=cfc('tools')->substr($item['desc'],0,'38');
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	$return['list']=$list?$list:array();
	exi($return);
}
if($_RQ['op']=='getimglist'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$fields='';
	$orderby=array();
	$_RQ['page'] = $_RQ['page'] ? $_RQ['page'] : 1;
	$pindex = max(1, intval($_RQ['page']));
	if(intval($_RQ['num'])){
		$psize = intval($_RQ['num']);
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_agentmcp_userimg',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['image']=tomedia($item['image']);
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	$return['list']=$list?$list:array();
	exi($return);
}
if($_RQ['op']=='addimgs'){
	$postdata=get_postdata();
	if(!$postdata['images']){
		exi('请上传图片！','error');
	}
	foreach($postdata['images'] as $img){
		$data=array(
			'image'=>trim($img),
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_agentmcp_userimg',$data);
	}
	exi('提交成功！','','/mcpitem//type=2&id='.$postdata['id']);
}
if($_RQ['op']=='addmp3'){
	$postdata=get_postdata();
	if(!$postdata['id']){
		exi('请选择音乐！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'artist'=>trim($postdata['artist']),
		'album'=>trim($postdata['album']),
		'genre'=>trim($postdata['genre']),
		'url'=>trim($postdata['url']),
		'groupid'=>trim($postdata['groupid']),
		'year'=>trim($postdata['year']),
		'mp3id'=>$postdata['id'],
		'goodsid'=>$postdata['id'],
	);
	$id=pdo_getcolumn('jy_agentmcp_usermp3',array('goodsid'=>$postdata['id'],'mid'=>$_SESSION['uid'],'mp3id'=>trim($postdata['id']),'uniacid'=>$_SESSION['uniacid']),'id');
	if($id){
		exi('该音乐您已添加过，无需重复！','error');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['mp3id']=$postdata['id'];
		$data['goodsid']=$_RQ['mcpid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_agentmcp_usermp3',$data);
	}
	exi('操作成功！','','/mcpitem//type=1&ismymp3=1&id='.$_RQ['mcpid']);
}
if($_RQ['op']=='upmcpurl'){
	if(!$_RQ['id']){
		exi('请选择设备~！','error');
	}
	$usergoods=pdo_get('jy_agentmcp_usergoods',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	$this->model('mcp')->bindMcpEndpoint($usergoods);
	exi('刷新MCP成功！');
}
